<template>
	<view class="index-content">
		<scroll-view scroll-x class="bg-white nav" scroll-with-animation :scroll-left="scrollLeft" style="position: fixed;z-index: 100;border-bottom: 1px solid ghostwhite;enable-flex:true">
			<view class="cu-item" :class="index==TabCur?'text-green cur':''" v-for="(item,index) in category" :key="index" @tap="tabSelect"
			 :data-id="index">
				<text :style="index==TabCur?'background: #FC3F78;color: white;padding: 4px 10px 4px 10px;border-radius: 16px;margin-right: 10px;margin-left:30px':'background: #ffffff;color: #333333;margin-right: 10px'">{{item.name}}</text>

			</view>
		</scroll-view>
		<view class="index-coupon has-bg-white has-pd-10 top_30">
			<view class="goods-list" v-if="couponlist.length > 0" style="padding-top: 46px">
				<orange-goods-card-home v-for="(item,index) in couponlist" :index="index%2" :itemid="item.goods_id" :logo='logo'
				 :itempic="item.goods_thumbnail_url?item.goods_thumbnail_url:'https://www.gomyorder.cn/logo.png'" :itemtitle="item.goods_name"
				 :itemprice='"¥"+item.tkmoney' :itemsale="'已售'+item.sales_tip+'件'" :itemendprice='""+(item.min_group_price/100.0)'
				 :couponmoney="(item.coupon_discount/100.0)"></orange-goods-card-home>
			</view>
		</view>
		<!-- 领券直播 -->

		<!-- 加载更多提示 -->
		<view class="s-col is-col-24" v-if="couponlist.length > 0">
			<load-more :loadingType="loadingType" :contentText="contentText"></load-more>
		</view>
		<!-- 加载更多提示 -->
		<!-- 悬浮上拉 -->
		<view class="scroll_top" @tap="topScrollTap" v-bind:class="[scrollTop ? 'active' : '','']" style="bottom: 56px;">
			<text class="iconfont icon-shangla"></text>
		</view>
		<empty v-if="couponlist.length === 0" des="暂无数据" show="false"></empty>
	</view>
</template>

<script>
	import Request from '../../utils/luch-request_1.0.6/request.js';
	const request = new Request();
	export default {
		onShareAppMessage(res) {
			return {
				title: '购物先领券，一年省一半',
				path: '/pages/index/index'
			}
		},
		data() {
			return {
				logo: "../../static/img/goods/hot.png",
				category: [{
						name: '1.9包邮'
					},
					{
						name: '今日爆款'
					},
					{
						name: '品牌清仓'
					},

				],
				TabCur: 0,
				scrollLeft: 0,
				scrollTop: false,
				couponlist: [],
				page: 0,
				list_id: '',
				search_id: '',
				pid: '',
				loadingType: 0,
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了"
				}
			}
		},
		onLoad: function(e) {
			this.TabCur = e.type;
			uni.showLoading({
				title: '加载中...'
			});
			this.loadCouponList();
			let userId = this.$queue.getData("userId");
			if (!userId) {
				this.$Request.getT("/user/" + userId).then(res => {
					if (res.status === 0) {
						this.$queue.setData("openid", res.data.openId);
						this.$queue.setData("image_url", res.data.image_url);
						this.$queue.setData("mobile", res.data.phone);
						this.$queue.setData("nickName", res.data.nickName);
						this.$queue.setData("relation_id", res.data.relationId);
						this.$queue.setData("gender", parseInt(res.data.gender));
					}
				});
			}
		},
		onPageScroll: function(e) {
			this.scrollTop = e.scrollTop > 200;
		},
		methods: {
			tabSelect(e) {
				uni.showLoading({
					title: '加载中...'
				});
				this.TabCur = e.currentTarget.dataset.id;
				console.log(e.currentTarget.dataset);
				this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60;
				this.page = 0;
				this.list_id = "";
				this.loadCouponList("Refresh");
				//#ifdef H5
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});
				//#endif
			},
			topScrollTap: function() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});
			},

			loadCouponList: function(type) {
				let that = this;
				that.loadingType = 1;
				console.log("tab"+that.TabCur);
				console.log("list_id"+that.list_id);
				request.post("/ddk/getGoods", {
					page: that.page*20,
					page_size: 20,
					list_id: that.list_id,
					type: that.TabCur,
					pid: that.pid
				}).then(res => {
					console.log(res);
					that.loadingType = 0;
					if (res.statusCode === 200) {
						if (that.page === 0) {
							that.couponlist = [];
						}
						that.list_id = res.data.goods_basic_detail_response.list_id;
						that.search_id = res.data.goods_basic_detail_response.search_id;
						let list = res.data.goods_basic_detail_response.list;
						list.forEach(d => {
							d.tkmoney = ((d.min_group_price * d.promotion_rate / 100000.0)).toFixed(2);
							that.couponlist.push(d);
						})
					} else {
						that.loadingType = 2;
					}
					uni.hideLoading();

					if (type === "Refresh") {
						uni.stopPullDownRefresh(); // 停止刷新
					}
				}).catch(err=>{
					console.log(err);
				})
			},
			toGoodsInfo: function(itemid) {
				uni.navigateTo({
					url: "/pages/detail/goodsinfo?itemid=" + item.goods_id + "&searchid=" + item.search_id + "&pid=" + item.p_id,
				})
			},
		},

		onReady: function() {},
		onReachBottom: function() {
			this.page = this.page + 1;
			this.loadCouponList();
		},
		onPullDownRefresh: function() {
			this.page = 1;
			this.min_id = 1;
			this.loadCouponList("Refresh");
		}
	}
</script>

<style>
	@import "../../static/css/index.css";
</style>
